<?php
namespace app\admin\controller;

use app\admin\service\HotSearch as HotSearchService;
use think\facade\Request;
use think\facade\View;

class HotSearch extends Controller
{
    public function index()
    {
        $type = input("type");
        if (Request::isAjax()) {
            $keyword = input("keyword");
            $condition[] = ['type', "=", $type];
            if(!empty($keyword)){
                $condition[] = ['keyword', 'like',"%".$keyword."%"];
            }

            $service = new HotSearchService();
            $cateList = $service->getList($condition);
            return $this->success("获取成功", $cateList);
        }else {
            View::assign('type', $type);
            return View::fetch();
        }
    }

    public function add()
    {
        $type = input("type");
        $service = new HotSearchService();
        if (Request::isAjax()) {
            $keyword = input("keyword");
            $sort = input("sort", 99);
            $status = input("status", 1);
            $data = [
                "keyword" => $keyword,
                "sort" => $sort,
                "status" => $status,
                "type" => $type,
            ];
            $res = $service->add($data);
            if (!$res) $this->error($service->getError());
            return $this->success('添加成功');
        }else {
            View::assign('type', $type);
            return View::fetch();
        }
    }

    public function edit()
    {
        $type = input("type");
        $service = new HotSearchService();
        if (Request::isAjax()) {
            $post = input();
            $res = $service->edit($post, ["id" => $post['id'], "type" => $type]);
            if (!$res) $this->error($service->getError());
            return $this->success('修改成功');
        } else {
            View::assign('type', $type);
            $id = input('id');
            if (empty($id)) return $this->error('非法操作');
            $info = $service->detail(["id" => $id, "type" => $type]);
            if (empty($info)) return $this->error('菜单不存在');
            View::assign('info', $info);
            return View::fetch('add');
        }
    }

    public function del()
    {
        if (Request::isAjax()) {
            $type = input("type");
            $ids = input('id');
            if (empty($ids)) return $this->error('请选择删除项');
            $service = new HotSearchService();
            $res = $service->del($ids, $type);
            if (!$res) $this->error($service->getError());
            return $this->success('删除成功');
        }
    }

}
